package xdclass.cloud;

import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import xdclass.cloud.param.MqConfigParam;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;

@Configuration
@EnableConfigurationProperties(MqConfigParam.class)
@ConditionalOnProperty(prefix = "mq.general", value = "enabled", matchIfMissing = true)
public class RabbitMqConfiguration {

    @Autowired
    private MqConfigParam mqConfigParam;
    @Autowired
    private AutowireCapableBeanFactory beanFactory;
    @Autowired
    private DefaultListableBeanFactory defaultListableBeanFactory;

    @PostConstruct
    public void mqBeanInit() {

        List<Map<String, Object>> lists = mqConfigParam.getList();
        lists.forEach(stringObjectMap -> {
            if(stringObjectMap.containsKey("queue") && stringObjectMap.containsKey("type") && "".equals(stringObjectMap.get("type"))) {
                Queue temp = new Queue((String) stringObjectMap.get("queue"),true);;
                //将new出的对象放入Spring容器中
                defaultListableBeanFactory.registerSingleton(stringObjectMap.get("simpleName").toString(),temp);
                //自动注入依赖
                beanFactory.autowireBean(temp);
            }
        });

    }


}
